package com.ct.book.service;

import com.ct.book.enums.BookStatus;
import com.ct.book.mapper.BookInfoMapper;
import com.ct.book.model.BookInfo;
import com.ct.book.model.PageRequest;
import com.ct.book.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookInfoMapper bookInfoMapper;
    public void addBook(BookInfo bookInfo) {
        bookInfoMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest) {
        Integer count = bookInfoMapper.count();
        List<BookInfo> books = bookInfoMapper.queryBookListByPage(pageRequest);
        for (BookInfo book : books) {
            book.setStatusCN(BookStatus.getNameByCode(book.getStatus()).getName());
        }
        return new PageResult<>(count, books, pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookInfoMapper.queryBookById(bookId);
    }

    public void updateBook(BookInfo bookInfo) {
        bookInfoMapper.updateBook(bookInfo);
    }

    public Integer deleteById(Integer bookId) {
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(bookId);
        bookInfo.setStatus(BookStatus.DELETED.getCode());
        return bookInfoMapper.updateBook(bookInfo);
    }


    public Integer batchDeleteById(List<Integer> ids) {
        return bookInfoMapper.batchDeleteById(ids);
    }
}
